<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Mongodb 占用服务器内存过大解决方案 | 智能技术</title>
    <meta name="generator" content="VuePress 1.8.2">
    <script data-ad-client="ca-pub-1977396037355374" async="true" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <meta name="description" content="mongodb内存占用过大解决方案">
    <meta name="baidu-site-verification" content="fP9EQkbGCM">
    
    <link rel="preload" href="/assets/css/0.styles.89695ccf.css" as="style"><link rel="preload" href="/assets/js/app.cee3c598.js" as="script"><link rel="preload" href="/assets/js/20.f186cf8f.js" as="script"><link rel="prefetch" href="/assets/js/10.81160393.js"><link rel="prefetch" href="/assets/js/11.777111f5.js"><link rel="prefetch" href="/assets/js/12.c58a9b41.js"><link rel="prefetch" href="/assets/js/13.16b64097.js"><link rel="prefetch" href="/assets/js/14.81b84b8b.js"><link rel="prefetch" href="/assets/js/15.ae9bbcfc.js"><link rel="prefetch" href="/assets/js/16.cb71ce72.js"><link rel="prefetch" href="/assets/js/17.7eb91f50.js"><link rel="prefetch" href="/assets/js/18.1f750554.js"><link rel="prefetch" href="/assets/js/19.525a894f.js"><link rel="prefetch" href="/assets/js/2.0a188998.js"><link rel="prefetch" href="/assets/js/21.b3e01dc7.js"><link rel="prefetch" href="/assets/js/22.ea0793c3.js"><link rel="prefetch" href="/assets/js/23.da81792d.js"><link rel="prefetch" href="/assets/js/24.0d075789.js"><link rel="prefetch" href="/assets/js/25.901ac41c.js"><link rel="prefetch" href="/assets/js/26.c3112033.js"><link rel="prefetch" href="/assets/js/27.0d727913.js"><link rel="prefetch" href="/assets/js/3.0d59f57e.js"><link rel="prefetch" href="/assets/js/4.58ac6eea.js"><link rel="prefetch" href="/assets/js/5.b48f4120.js"><link rel="prefetch" href="/assets/js/6.6a7eb352.js"><link rel="prefetch" href="/assets/js/7.0a07ef26.js"><link rel="prefetch" href="/assets/js/8.0332c4f5.js"><link rel="prefetch" href="/assets/js/9.9df962ab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.89695ccf.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><section id="global-layout" data-v-4fb7124e><header class="header" data-v-61b62cbe data-v-4fb7124e><div class="header-navbar" data-v-61b62cbe><div class="flex-xbc main header-nav" data-v-61b62cbe><div class="nav-link" data-v-61b62cbe><!----> <nav class="link-list" data-v-61b62cbe><a href="/" class="list-item router-link-active" data-v-61b62cbe>首页</a><a href="/posts/" class="list-item" data-v-61b62cbe>博客</a><a href="/category/" class="list-item" data-v-61b62cbe>分类</a><a href="/tag/" class="list-item" data-v-61b62cbe>标签</a><a href="/about/" class="list-item" data-v-61b62cbe>关于</a></nav></div> <div class="search-box" data-v-61b62cbe><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div> </header> <!----> <section class="page" data-v-4fb7124e data-v-4fb7124e><section class="info" style="background-image:url(/images/timg.jpeg);" data-v-52fe94f0><article class="main info-content" data-v-a5c9dc12 data-v-52fe94f0><div class="content-header" data-v-a5c9dc12><h1 class="header-title" data-v-a5c9dc12>Mongodb 占用服务器内存过大解决方案</h1></div> <div class="flex-wcc content-tag" data-v-a5c9dc12><div class="inblock tag-list" data-v-a5c9dc12><a href="/category/问题解决/" class="tag-text" data-v-a5c9dc12>问题解决
      </a></div> <span class="tag-space" data-v-a5c9dc12>/</span> <div class="inblock tag-list" data-v-a5c9dc12><a href="/tag/mongodb/" class="tag-text" data-v-a5c9dc12>mongodb
      </a></div></div> <div class="content content__default" data-v-a5c9dc12><p>mongodb开启的数据库默认会占用服务器一半的内存，用来缓存可能频繁查询的内容，以加快查询速度。而我们日常使用的服务器不只是用来开启一个mongodb数据库，还会有其他的用途。这就需要限制mongodb数据库的内存占用。</p> <h3 id="_1-运行中的mongodb内存释放"><a href="#_1-运行中的mongodb内存释放" class="header-anchor">#</a> 1. 运行中的mongodb内存释放</h3> <p>目前，正在运行的mongodb没有很好的内存回收机制，如果想回收内存，只能进入mongodb的admin数据库关闭所有数据库</p> <div class="language- extra-class"><pre class="language-text"><code>mongo&gt; use admin 
mongo&gt; db.runCommand({closeAllDatabases:1})
</code></pre></div><h3 id="_2-修改mongodb的配置文件"><a href="#_2-修改mongodb的配置文件" class="header-anchor">#</a> 2. 修改mongodb的配置文件</h3> <p>还有一种方法是开启数据库时，指定mongodb的配置文件</p> <div class="language- extra-class"><pre class="language-text"><code>./mongod -f /etc/mongodb.conf
</code></pre></div><p>在配置文件中设置内存占用，配置文件修改如下内容：</p> <div class="language- extra-class"><pre class="language-text"><code>storage:
    dbPath: &quot;/data/mongodb/data”   #数据目录
    directoryPerDB: true      #将不同DB的数据分子目录存储，基于dbPath，默认为 false
    engine: “wiredTiger&quot;      #存储引擎，3.2后默认wiredTiger 可选 mmapv1
    wiredTiger:
        engineConfig:
            cacheSizeGB: 15     #默认最大缓存15GB。
</code></pre></div><h3 id="_3-开启mongodb时增加内存占用参数"><a href="#_3-开启mongodb时增加内存占用参数" class="header-anchor">#</a> 3. 开启mongodb时增加内存占用参数</h3> <p>建议使用这种方案，在开启mongodb时添加 <code>--wiredTigerCacheSizeGB 15</code> 参数即可。表示mongodb最大内存限制15GB</p></div> <div class="content-time" data-v-a5c9dc12><time datetime="2020年8月6日" class="time-text" data-v-a5c9dc12>Create Time: 2020年8月6日
    </time> <time datetime="2020年9月13日" class="time-text" data-v-a5c9dc12>Last Updated: 2020年9月13日
    </time></div></article> <section class="flex-xb main info-nav" data-v-e08c9474 data-v-52fe94f0><a href="/2020/09/13/%E6%AF%94overleaf%E5%A5%BD%E7%94%A8%E7%9A%84latex%E7%BC%96%E8%BE%91%E5%99%A8vscode-texlive-git/" class="flex-xb nav-item" data-v-e08c9474><div class="flex-xcc item-img" data-v-e08c9474><img data-src="/images/12.jpg" loading="lazy" alt="比overleaf好用的latex编辑器vscode+texlive+git" class="img lazy" data-v-e08c9474></div> <article class="flex-ysc item-content" data-v-e08c9474><h2 class="content-title" data-v-e08c9474>比overleaf好用的latex编辑器vscode+texlive+git</h2> <div class="content" data-v-e08c9474></div></article></a> <a href="/2020/06/14/%E4%BF%A1%E6%81%AF%E4%BC%A0%E6%92%AD%E6%95%B0%E6%8D%AE%E9%9B%86%E6%95%B4%E7%90%86/" class="flex-xb nav-item" data-v-e08c9474><div class="flex-xcc item-img" data-v-e08c9474><img data-src="/images/542115a877c26.jpg" loading="lazy" alt="信息传播数据集整理" class="img lazy" data-v-e08c9474></div> <article class="flex-ysc item-content" data-v-e08c9474><h2 class="content-title" data-v-e08c9474>信息传播数据集整理</h2> <div class="content" data-v-e08c9474></div></article></a></section> <!----></section></section> <footer class="footer" data-v-5bc4f524 data-v-4fb7124e><nav class="link-list" data-v-5bc4f524><a href="https://gitee.com/tjuchaochao" target="_blank" rel="noopener noreferrer" class="list-item" data-v-5bc4f524>gitee</a></nav> <a href="/" class="copyright router-link-active" data-v-5bc4f524>智能技术 © 2021</a></footer></section><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.cee3c598.js" defer></script><script src="/assets/js/20.f186cf8f.js" defer></script>
  </body>
</html>
